package cn.xiongxf.transactional;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 1.实例化消息生产者Producer
        //DefaultMQProducer producer = new DefaultMQProducer("sync_producer_group");
        TransactionMQProducer producer = new TransactionMQProducer("transactional_producer_group");
        // 2.设置NameServer的地址
        producer.setNamesrvAddr("localhost:9876");

        // 设置监听者
        producer.setTransactionListener(new MyListener());

        // 3.启动生产者
        producer.start();
        System.out.println("生产者启动。。。");
        // 4.发送消息到一个Broker
        Message msg = new Message("Topic_transactional", "Part_1", "你好，RocketMQ!".getBytes(RemotingHelper.DEFAULT_CHARSET));
        SendResult sendResult = producer.sendMessageInTransaction(msg,null);
        System.out.println(sendResult);
        // 5.如果不再发送消息，关闭Producer实例
        producer.shutdown();
    }
}